package com.home.homepage.service.link.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eig.common.entity.homepageentity.link.TbLink;
import com.eig.common.vo.Result;
import com.home.homepage.dao.link.LinkDao;
import com.home.homepage.service.link.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @author ：YQ袁（2446806940@qq.com）
 * @date ：Created in 2022/12/23 17:06
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class LinkServiceImpl implements LinkService {
    @Autowired
    private LinkDao linkDao;

    @Override
    public Result getAllLink(Integer currentPage, Integer pageSize, TbLink tbLink) {
        IPage<TbLink> page= new Page<>(currentPage,pageSize);
        QueryWrapper<TbLink> wrapper= new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        if(StringUtils.hasText(tbLink.getLinkUrl())){
            wrapper.like("link_url" ,tbLink.getLinkUrl());
        }
        if(StringUtils.hasText(tbLink.getLinkName())){
            wrapper.like("link_name" ,tbLink.getLinkName());
        }
        IPage<TbLink> page1 = linkDao.selectPage(page, wrapper);
        return new Result(2000,"查询成功",page1);
    }

    @Override
    public Result changeStatus(String linkId, String isDisable) {
        UpdateWrapper<TbLink> wrapper = new UpdateWrapper<>();
        wrapper.eq("link_id",linkId);
        wrapper.set("is_disable",isDisable);
        int update = linkDao.update(null, wrapper);
        if (update>0){
            return new Result(2000,"修改状态成功");
        }
        return new Result(5000,"修改状态失败");
    }

    @Override
    public Result deleteLink(TbLink tbLink) {
        UpdateWrapper<TbLink> wrapper = new UpdateWrapper<>();
        wrapper.eq("link_id",tbLink.getLinkId());
        int delete = linkDao.delete(wrapper);
        if (delete>0){
            return  new Result(2000,"删除成功");
        }
        return new Result(5000,"删除失败");

    }

    @Override
    public Result addLink(TbLink tbLink) {
            TbLink tb = new TbLink();
            tb.setLinkName(tbLink.getLinkName());
            tb.setLinkUrl(tbLink.getLinkUrl());
            tb.setIsDeleted(0);
            tb.setIsDisable(0);
            tb.setGmtCreate(new Date());
            tb.setGmtModified(new Date());
            int insert = linkDao.insert(tb);
            if (insert>0){
                return new Result(2000,"添加成功");
            }
            return new Result(5000,"添加失败");
    }

    @Override
    public Result updateLink(TbLink tbLink) {
        UpdateWrapper<TbLink> wrapper = new UpdateWrapper<>();
        wrapper.eq("link_id",tbLink.getLinkId());
        TbLink tb = new TbLink();
        tb.setLinkName(tbLink.getLinkName());
        tb.setLinkUrl(tbLink.getLinkUrl());
        int update = linkDao.update( tb,wrapper);
        if (update>0){
            return new Result(2000,"修改成功");
        }
        return new Result(5000,"修改失败");

    }
}
